// pages/ordetails/ordetails.js
let app = getApp()
let api = require("../../utils/api.js")


var MD5 = require('../../utils/md5.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Type: 1,
        order_sn: "",
        orderInfo: {},
        pay_status: 0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this
        let order_sn = options.order_sn
        let data = {
            order_sn: order_sn,
        }
        let url = app.u.HOME_URL + app.u.ORDER_DETAILS
        api.requestUrl(data, url).then(res => {
            if (res.code == 200) {
                that.setData({
                    orderInfo: res.data,
                    pay_status: res.data.pay_status,
                    order_sn: order_sn,
                })
            } else {
                api.showError(res.msg)
                setTimeout(function() {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1000)
            }
        })
    },
    cancel: function(e) {
        let that = this
        wx.showModal({
            title: '友情提示',
            content: '确定取消该订单？',
            success(res) {
                if (res.confirm) {
                    let data = { order_sn: e.currentTarget.dataset.ordersn }
                    let url = app.globalData.resBaseUrl + app.u.ORDER_CANCEL
                    api.requestUrl(data, url).then(res => {
                        api.showSuccess("取消成功")
                        setTimeout(function() {
                            wx.navigateBack({ delta: 1 })
                        }, 1000)
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    gopay: function(e) {
        let that = this
        let ordersn = that.data.order_sn
        let opendid = wx.getStorageSync('openId')
        let data = { openId: opendid, order_sn: ordersn }
        let url = app.globalData.resBaseUrl + app.u.ORDER_PAY
        api.requestUrl(data, url).then(res => {
            console.log(res);
            let prepay_id = res.data.prepay_id
            if (res.code == 200) {
                let nonceStr = res.data.parameters.nonceStr
                let apackage = res.data.parameters.package
                let paySign = res.data.parameters.paySign
                let signType = res.data.parameters.signType
                let timeStamp = res.data.parameters.timeStamp
                let appId = res.data.appid
                let paySignMD5 = MD5.hex_md5(`appId=${appId}&nonceStr=${nonceStr}&package=${apackage}&signType=${signType}&timeStamp=${timeStamp}&key=jiuzhouxinli1234jiuzhouxinli1234`).toUpperCase()
                if (paySign === paySignMD5) {
                    wx.requestPayment({
                        timeStamp: timeStamp,
                        nonceStr: nonceStr,
                        package: apackage,
                        signType: signType,
                        paySign: paySign,
                        success: function(res) {
                            api.showError("支付成功")
                            that.setData({
                                pay_status: 2
                            })
                            let data = { prepay_id: prepay_id, openid: opendid, order_sn: ordersn }
                            let sendurl = app.u.HOME_URL + app.u.SENDMESG
                            api.requestUrl(data, sendurl).then(res => {
                                console.log(res)
                            })
                        },
                        fail: function(res) {
                            api.showError("支付失败")
                        },
                    })
                } else {
                    api.showError('签名错误')
                }
            } else {
                api.showError(res.msg)
            }

        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})